﻿using System;

namespace XNET.Extension
{
    /// <summary>
    /// 时间扩展
    /// </summary>
    public static class TimeSpanExtension
    {
        /// <summary>
        /// 将时间间隔转换为天数
        /// </summary>
        /// <param name="Span"></param>
        /// <returns></returns>
        public static int Days(this TimeSpan Span)
        {
            return (DateTime.MinValue + Span).Day - 1;
        }

        /// <summary>
        /// 将时间间隔转换为月份
        /// </summary>
        /// <param name="Span">时间间隔</param>
        /// <returns></returns>
        public static int Months(this TimeSpan Span)
        {
            return (DateTime.MinValue + Span).Month - 1;
        }

        /// <summary>
        /// 将时间间隔转换为年
        /// </summary>
        /// <param name="Span">时间间隔</param>
        /// <returns></returns>
        public static int Years(this TimeSpan Span)
        {
            return (DateTime.MinValue + Span).Year - 1;
        }

        /// <summary>
        /// 格式化时间(CN)
        /// </summary>
        /// <param name="Input">时间间隔</param>
        /// <returns></returns>
        public static string ToStringFullCN(this TimeSpan Input)
        {
            var Result = string.Empty;
            var Splitter = string.Empty;
            if (Input.Years() > 0)
            {
                Result += Input.Years() + " 年";
                Splitter = ", ";
            }
            if (Input.Months() > 0)
            {
                Result += Splitter + Input.Months() + " 月";
                Splitter = ", ";
            }
            if (Input.Days() > 0)
            {
                Result += Splitter + Input.Days() + " 天";
                Splitter = ", ";
            }
            if (Input.Hours > 0)
            {
                Result += Splitter + Input.Hours + " 时";
                Splitter = ", ";
            }
            if (Input.Minutes > 0)
            {
                Result += Splitter + Input.Minutes + " 分";
                Splitter = ", ";
            }
            if (Input.Seconds > 0)
            {
                Result += Splitter + Input.Seconds + " 秒";
                Splitter = ", ";
            }
            return Result;
        }

        /// <summary>
        /// 格式化时间(EN)
        /// </summary>
        /// <param name="Input">时间间隔</param>
        /// <returns>The TimeSpan as a string</returns>
        public static string ToStringFullEN(this TimeSpan Input)
        {
            var Result = string.Empty;
            var Splitter = string.Empty;
            if (Input.Years() > 0)
            {
                Result += Input.Years() + " year" + (Input.Years() > 1 ? "s" : string.Empty);
                Splitter = ", ";
            }
            if (Input.Months() > 0)
            {
                Result += Splitter + Input.Months() + " month" + (Input.Months() > 1 ? "s" : string.Empty);
                Splitter = ", ";
            }
            if (Input.Days() > 0)
            {
                Result += Splitter + Input.Days() + " day" + (Input.Days() > 1 ? "s" : string.Empty);
                Splitter = ", ";
            }
            if (Input.Hours > 0)
            {
                Result += Splitter + Input.Hours + " hour" + (Input.Hours > 1 ? "s" : string.Empty);
                Splitter = ", ";
            }
            if (Input.Minutes > 0)
            {
                Result += Splitter + Input.Minutes + " minute" + (Input.Minutes > 1 ? "s" : string.Empty);
                Splitter = ", ";
            }
            if (Input.Seconds > 0)
            {
                Result += Splitter + Input.Seconds + " second" + (Input.Seconds > 1 ? "s" : string.Empty);
                Splitter = ", ";
            }
            return Result;
        }
    }
}
